package cn.yitun.chat.yitunserverimapicore.business.contact.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactRelationDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactRelationVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.contact.service.ContactRelationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 17:39:11<br>
 * Description
 *
 * @author yitun<br>
 * @version 1.1
 */
@Component
@ActionMapping(value = ActionTypeConstant.CONTACT_RELATION_ACTION_PUSH)
public class ContactRelationAction {

	@Resource
	private ContactRelationService contactCategoryMemberService;

	@ActionMapping(value = "1.1.0001")
	public ResultMessage getCount(SocketSession socketSession) {
		Long ownerUserId = socketSession.getKey();

		return new ResultMessage(R.param("count", contactCategoryMemberService.countByOwnerUserId(ownerUserId)));
	}

	@ActionMapping(value = "1.1.0002")
	public ResultMessage getList(SocketSession socketSession) {
		Long ownerUserId = socketSession.getKey();
		return new ResultMessage(R.param("list", contactCategoryMemberService.getListByOwnerUserId(ownerUserId)));
	}

	@ActionMapping(value = "1.1.0004")
	public ResultMessage getRelation(SocketSession socketSession, @JsonDefine("data") ContactRelationDto query) {
		if (NumUtil.isInValid(query.getContactUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long ownerUserId = socketSession.getKey();
		query.setOwnerUserId(ownerUserId);

		ContactRelationVo data = contactCategoryMemberService.getByContactUserId(query);
		ResultMessage message = new ResultMessage();
		message.bodyPut("contactRelation", data);
		return message;
	}

	/**
	 * 
	 * Date 2019-01-20 17:37:06<br>
	 * Description 修改好友备注
	 * 
	 * @param socketSession
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0005")
	public ResultMessage updateContactRemark(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") ContactRelationDto query) {
		if (NumUtil.isInValid(query.getContactUserId()) || YitunStringUtil.isEmpty(query.getRemark()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setOwnerUserId(socketSession.getKey());
		return contactCategoryMemberService.updateRemark(head.getKey(), query);
	}

	/**
	 * 
	 * Date 2019-01-20 17:37:33<br>
	 * Description 移动好友
	 * 
	 * @param socketSession
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage moveCategory(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") ContactRelationDto query) {
		if (NumUtil.isInValid(query.getCategoryId()) || YitunCollectionUtil.isEmpty(query.getContactUserIds()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setOwnerUserId(socketSession.getKey());
		return contactCategoryMemberService.moveCategory(head.getKey(), query);
	}

	/**
	 * 
	 * Date 2019-01-20 17:38:23<br>
	 * Description 删除好友
	 * 
	 * @param socketSession
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0007")
	public ResultMessage delete(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") ContactRelationDto query) {
		if (NumUtil.isInValid(query.getContactUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setOwnerUserId(socketSession.getKey());
		return contactCategoryMemberService.delete(head.getKey(), query);
	}

	/**
	 * 
	 * Description 拉入或者移除黑名单 <br>
	 * Date 2019-05-08 21:01:16<br>
	 * 
	 * @param socketSession
	 * @param head
	 * @param query
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0008")
	public ResultMessage updateBlocked(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") ContactRelationDto query) {
		if (NumUtil.isInValid(query.getContactUserId()) || YitunStringUtil.isEmpty(query.getIsBlocked()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setOwnerUserId(socketSession.getKey());
		return contactCategoryMemberService.updateBlocked(head.getKey(), query);
	}
}
